package com.echo.register.msm.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.aliyuncs.DefaultAcsClient;
import com.aliyuncs.IAcsClient;
import com.aliyuncs.dysmsapi.model.v20170525.SendSmsRequest;
import com.aliyuncs.dysmsapi.model.v20170525.SendSmsResponse;
import com.aliyuncs.exceptions.ClientException;
import com.aliyuncs.exceptions.ServerException;
import com.aliyuncs.profile.DefaultProfile;
import com.echo.register.common.exception.RegisterException;
import com.echo.register.common.result.ResultCodeEnum;
import com.echo.register.msm.service.MsmService;
import com.echo.register.msm.utils.ConstantPropertiesUtils;
import com.echo.register.vo.msm.MsmVo;
import com.google.gson.Gson;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.HashMap;
import java.util.Map;

/**
 * @ClassName: MsmServiceImpl
 * @Package: com.echo.register.service.impl
 * @Description:
 * @Author 谢同学
 * @Create 2023/12/28 19:58
 * @Version 1.0
 */
@Service
public class MsmServiceImpl implements MsmService {


    /**
     * 发送手机验证码
     * @param phone 手机号
     * @param code  验证码
     * @return
     */
    @Override
    public boolean send(String phone, String code) {
        Map<String, Object> map = new HashMap<>();
        map.put("code", code);

        return this.send(phone, map);
    }

    /**
     * 发送订单信息
     *
     * @param msmVo 短信体
     * @return
     */
    @Override
    public boolean send(MsmVo msmVo) {
        return this.send(msmVo.getPhone(), msmVo.getParam());
    }

    private Boolean send(String phone,Map<String,Object> param){
        // 校验数据
        if (StringUtils.isEmpty(phone) || StringUtils.isEmpty(param)) {
            throw new RegisterException(ResultCodeEnum.PARAM_ERROR);
            // return false;
        }
        // 获取profile对象
        DefaultProfile profile = DefaultProfile.getProfile(ConstantPropertiesUtils.REGION_Id,
                ConstantPropertiesUtils.ACCESS_KEY_ID,
                ConstantPropertiesUtils.SECRECT);

        // 创建客户端
        IAcsClient client = new DefaultAcsClient(profile);

        // 创建发送短信的请求对象
        SendSmsRequest request = new SendSmsRequest();
        // 签名名称
        request.setSignName("阿里云短信测试");
        // 模板code
        request.setTemplateCode("SMS_154950909");
        // 手机号
        request.setPhoneNumbers(phone);
        // 模板参数
    /*    Map<String, Object> map = new HashMap<>();
        map.put("code", code);*/
        request.setTemplateParam(JSONObject.toJSONString(param));
        try {

            // 发送短信
            SendSmsResponse response = client.getAcsResponse(request);
            System.out.println(new Gson().toJson(response));
            // 校验状态码（
         /*   if ("OK".equals(response.getCode())) {
                System.out.println("向手机号:"+phone+"成功发送短信");
                return true;
            }*/

            // 测试使用(防止频繁测试发送验证码）
            System.out.println("向手机号:" + phone + "成功发送短信："+JSONObject.toJSONString(param));
            return true;

        } catch (ServerException e) {
            e.printStackTrace();
        } catch (ClientException e) {
            System.out.println("ErrCode:" + e.getErrCode());
            System.out.println("ErrMsg:" + e.getErrMsg());
            System.out.println("RequestId:" + e.getRequestId());
        }
        return false;
    }

}
